package com.jwsoft.manager.core.task.address;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jwsoft.manager.common.enums.DataSaveEnum;
import com.jwsoft.manager.common.vo.eduAddressTree.AddressVO;
import com.jwsoft.manager.common.vo.eduAddressTree.EduAddressTreeVO;
import com.jwsoft.manager.core.dao.model.EduAddress;
import com.jwsoft.manager.core.dao.model.EduAddressSchool;
import com.jwsoft.manager.core.dao.model.EduAddressSchoolAudit;
import com.jwsoft.manager.core.dao.service.EduAddressSchoolAuditService;
import com.jwsoft.manager.core.dao.service.EduAddressSchoolService;
import com.jwsoft.manager.core.dao.service.EduAddressService;
import com.jwsoft.manager.core.integration.eduAddressTree.AddressTreeObserver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 地址树treeId更新器
 *
 * @author fangs
 */
@Slf4j
@Service
public class UpdateTreeIdObserver implements AddressObserver, AddressTreeObserver {

    @Autowired
    private EduAddressSchoolService eduAddressSchoolService;
    @Autowired
    private EduAddressSchoolAuditService eduAddressSchoolAuditService;
    @Autowired
    private EduAddressService eduAddressService;

    @Override
    public void invoke(AddressVO addressVO) {
        if (StringUtils.hasText(addressVO.getEduAddressId())) {
            updateEduAddressSchool(addressVO.getEduAddressId(), addressVO.getAddressId());
            updateEduAddressSchoolAudit(addressVO.getEduAddressId(), addressVO.getAddressId());
        }
    }

    @Override
    @Async
    public void invoke(List<EduAddressTreeVO> changeList) {
        for (EduAddressTreeVO addressTreeVO : changeList) {
            if (!StringUtils.hasText(addressTreeVO.getEduAddressId())) {
                continue;
            }
            if (addressTreeVO.getChangeType() == DataSaveEnum.delete) {
                continue;
            }
            for (String s : addressTreeVO.getEduAddressIdList()) {
                UpdateWrapper<EduAddress> addressUpdateWrapper = new UpdateWrapper<>();
                addressUpdateWrapper.lambda().eq(EduAddress::getAddressId, s)
                        .set(EduAddress::getAddressTreeId, addressTreeVO.getAddressId());
                eduAddressService.update(addressUpdateWrapper);

                updateEduAddressSchool(s, addressTreeVO.getAddressId());
                updateEduAddressSchoolAudit(s, addressTreeVO.getAddressId());
            }
        }
    }


    private boolean updateEduAddressSchool(String addressId, Long treeId) {
        QueryWrapper<EduAddressSchool> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EduAddressSchool::getAddressId, addressId);
        if (eduAddressSchoolService.count(queryWrapper) == 0) {
            return false;
        }

        UpdateWrapper<EduAddressSchool> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(EduAddressSchool::getAddressId, addressId)
                .set(EduAddressSchool::getTreeId, treeId);
        return eduAddressSchoolService.update(updateWrapper);
    }

    private boolean updateEduAddressSchoolAudit(String addressId, Long treeId) {
        QueryWrapper<EduAddressSchoolAudit> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EduAddressSchoolAudit::getAddressId, addressId);
        if (eduAddressSchoolAuditService.count(queryWrapper) == 0) {
            return false;
        }

        UpdateWrapper<EduAddressSchoolAudit> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(EduAddressSchoolAudit::getAddressId, addressId)
                .set(EduAddressSchoolAudit::getTreeId, treeId);
        return eduAddressSchoolAuditService.update(updateWrapper);
    }


}
